//
//  ButtonViewController.m
//  Streaming
//
//  Created by 韩一博 on 15/6/26.
//  Copyright (c) 2015年 liuxun. All rights reserved.
//

#import "ButtonViewController.h"

@interface ButtonViewController ()

@end

@implementation ButtonViewController
- (void)dealloc
{
    [self.player release];
    [self.movieUrl release];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createVideo];
    
}
//- (void)leftBarButtonItemAction
//{
//    [self.player  stop];
//    [self.navigationController dismissViewControllerAnimated:YES completion:^{
//        
//    }];
//}



-(void)createVideo
{
    //把视频接口的网址接过来
    //    NSString *videoStr = str;
    //用新的接口将视频数据请求下来
    self.player =[[MPMoviePlayerController alloc]init];
    [self.player  stop];
    //添加 AirPlay
    //    self.player.allowsAirPlay =YES;
    [self.player.view setFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
    self.player.contentURL = [NSURL URLWithString:self.movieUrl];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    //view旋转
    [self.view setTransform:CGAffineTransformMakeRotation(M_PI/2)];
    //    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
        // iOS 7
        [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    }
    
//    //是否开启全屏
//    self.player.fullscreen = NO;
//    self.player.repeatMode =  MPMovieRepeatModeNone;//播放模式
    
    //设置视频播放的适配
    self.player.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    
    //播放状态的改变
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(backStateDidChangeNotification) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.player];
    
    
    self.player.controlStyle = MPMovieControlStyleFullscreen;
    
    
    [self.player play];
    [self.view addSubview:self.player.view];
    
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(exit) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    
    //监听视频播放完成
    [center addObserver:self selector:@selector(exit) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
}
-(void)exit{
    NSLog(@"%s",__func__);
    //退出当前的控制器
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    if (![self.player isPreparedToPlay]) {
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}


- (void)backStateDidChangeNotification
{
    NSLog(@"111");
}


-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{
    switch (self.player.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放...");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放.");
            break;
        case MPMoviePlaybackStateStopped:
            [self.player stop];
            break;
        default:
            NSLog(@"播放状态:%i",self.player.playbackState);
            break;
    }
}

-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{
    NSLog(@"播放完成.%i",self.player.playbackState);
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self.player stop];
    self.player = nil;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
